var ajaxResult;
var intakes = 34;
var v  = 0;

function hideMenu(){
	$("ul#std_info").css({"display":"none" });
}

function statusCheck(){
				jsonT = jQuery.parseJSON(ajaxResult.responseText);
		
				if(jsonT.stds.length > 0) $("div#students-container div.thumbnail").children().not(".loading").remove();
				
				var newChildren = "";
				
				for( i = 0 ; i < jsonT.stds.length ; i++ ) {
					var element = "<div class='thumbnail item pull-left' > \
							<div class='img-thumbnail-height' > \
								<img src='"+jsonT.stds[i].img+"' alt='Graduate Image' class='img-circle' /> \
							</div> \
							<h3><a href='graduateprofile.html' id='"+i+"' >"+jsonT.stds[i].name+"</a></h3> \
							<span>"+jsonT.stds[i].track+" - intake "+jsonT.stds[i].intake+"</span><br/> \
							<span>"+jsonT.stds[i].job+"</span> \
						</div>";
						
					newChildren += element;
				}
				
				$("div#students-container").append(newChildren).fadeIn(1000);
				
				$("ul.pagination").html("");
				for(i = 0 ; i < jsonT.pagesNo ; i++){
					$("ul.pagination").append("<li><a href='#'>" + (i+1) + "</a></li>");
				}
				$("ul.pagination").fadeIn(100);
				
				$("div.thumbnail h3 a").on("mouseenter",function(){
					clearTimeout(v);
					var p  = $(this).position();
					var id = $(this).attr("id");
					$("ul#std_info").delay(100).css({"top": (p.top+25) , "left": (p.left +50) , "display":"block" });
					$("ul#std_info").html("");
					
					if( jQuery.trim(jsonT.stds[id].mail) != "") $("ul#std_info").append("<li> <b>E-Mail : </b>" + jsonT.stds[id].mail +"</li>");
					if( jQuery.trim(jsonT.stds[id].mobile) != "") $("ul#std_info").append("<li role='presentation' class='divider'></li><li> <b>Mobile : </b>" + jsonT.stds[id].mobile +"</li>");
				});
				
				$("ul#std_info").on("mouseenter",function(){
					clearTimeout(v);
				});
				
				$("div.thumbnail h3 a , ul#std_info").on("mouseleave",function(){
					v = setTimeout("hideMenu()",500);
				});
			
}

function hide(){
	$("div#students-container").children().not(".loading").hide();
	$("ul.pagination").hide();
}

for(i = 1 ; i <= intakes ; i++){
	$( "select#intake" ).append("<option value='"+i+"'  >"+i+"</option>");
}

$( "form select#track" ).change(function() {
	if( $(this).val() == "1" ){
		ajaxResult = $.ajax({url:"http://localhost/os/open_source.txt",beforeSend: hide ,success: statusCheck});
	}				
});	
		
$( "#intake" ).change(function() {
	if( $(this).val() == 34 ){
		ajaxResult = $.ajax({url:"http://localhost/os/open_source_34.txt",beforeSend: hide ,success: statusCheck});		
	}			
});	
		
$( "#name" ).change(function() {
	if( $(this).val().charAt(0) == "A" ){
		ajaxResult = $.ajax({url:"http://localhost/os/open_source_std_a.txt",beforeSend: hide , success: statusCheck});	
		return false;
	}			
});	


$("form#search").submit(function(){ return false; });





